/*
 * Copyright (C) 2024 Google LLC.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.copybara.http.endpoint;

import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.api.client.http.HttpContent;
import com.google.copybara.credentials.CredentialIssuingException;
import com.google.copybara.credentials.CredentialRetrievalException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/** Itercepts HTTP content and resolves secrets. */
class HttpContentInterceptor implements HttpContent {
  private final HttpContent content;
  private final String outputStr;

  public HttpContentInterceptor(HttpContent content, HttpSecretInterceptor secretInterceptor)
      throws IOException, CredentialIssuingException, CredentialRetrievalException {
    this.content = content;
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    if (content != null) {
      content.writeTo(stream);
    }
    String httpContent = stream.toString(UTF_8);
    this.outputStr = secretInterceptor.resolveStringSecrets(httpContent);
  }

  @Override
  public long getLength() throws IOException {
    return outputStr.length();
  }

  @Override
  public String getType() {
    return content.getType();
  }

  @Override
  public boolean retrySupported() {
    return content.retrySupported();
  }

  @Override
  public void writeTo(OutputStream out) throws IOException {
    out.write(outputStr.getBytes(UTF_8));
    out.flush();
  }
}
